package com.itfreer.gis.map.resource;

import com.itfreer.form.api.DictionaryServiceImp;
import com.itfreer.form.api.dictionary.DictionaryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.alibaba.dubbo.config.annotation.Service;
import com.itfreer.form.api.BaseDao;
import com.itfreer.gis.map.resource.dao.CustomServiceDao;
import com.itfreer.gis.map.resource.entity.CustomServiceEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 定义我的服务实现类
 */
@Component
@Service(interfaceClass = CustomServiceService.class, path = "giscustomservice")
public class CustomServiceServiceImp extends DictionaryServiceImp<CustomServiceEntity> implements CustomServiceService {

	@Autowired
	private CustomServiceDao dao;

	@Override
	protected BaseDao<CustomServiceEntity> getDao() {
		return dao;
	}

	@Override
	public String getDictionaryName() {
		return "g_custom_service_config";
	}

	/**
	 * 是否为租户间共享实体对象
	 */
	protected boolean isShareTenant() {
		return true;
	}

	/**
	 * 获取租户间共享字段名称 用于SAAS下数据管理
	 */
	protected String getShareTenantFieldName() {
		return "shareTenantID";
	}

	@Override
	protected DictionaryEntity convert(CustomServiceEntity entity) {
		DictionaryEntity toEntity = new DictionaryEntity();
		toEntity.setId(entity.getId());
		toEntity.setCode(entity.getCname());
		toEntity.setDisplay(entity.getName());
		toEntity.setInfo(entity.getInfo());
		toEntity.setValid(entity.getEnable());
		toEntity.setOrder(entity.getOrder());
		toEntity.setTenantID(entity.getTenantID());
		toEntity.setTenantName(entity.getTenantName());
		toEntity.setShareTenantID(entity.getShareTenantID());

		Map<String, Object> other = new HashMap<String, Object>();
		other.put("type", entity.getType());
		other.put("name", entity.getCname());
		other.put("serverType", entity.getServerType());
		other.put("propertys", entity.getOriginal());
		other.put("metedata", entity.getMetedata());
		other.put("schemes", entity.getServiceSchemes());
		toEntity.setOther(other);

		return toEntity;
	}

	@Override
	protected String getDictionaryFields() {
		return "*";
	}

	/**
	 * 共享
	 * 
	 * @param id
	 * @param shareTenantID
	 * @param shareTenantName
	 * @return
	 */
	public Boolean share(String id, String shareTenantID, String shareTenantName) {
		CustomServiceEntity entity = getEntity(id);
		if (entity == null) {
			return true;
		}

		// 配置事务策略
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setName("planOne-transaction");
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		// 设置状态点
		TransactionStatus status = transactionManager.getTransaction(def);
		Boolean result = false;
		try {
			if (entity.getShareTenantID() == null || entity.getShareTenantID().equals("")) {
				entity.setShareTenantID("," + shareTenantID + ",");
			} else {
				if (entity.getShareTenantID().indexOf("," + shareTenantID + ",") == -1) {
					entity.setShareTenantID(entity.getShareTenantID() + shareTenantID + ",");
				}
			}
			if (entity.getShareTenantName() == null || entity.getShareTenantName().equals("")) {
				entity.setShareTenantName("," + shareTenantName + ",");
			} else {
				if (entity.getShareTenantName().indexOf("," + shareTenantName + ",") == -1) {
					entity.setShareTenantName(entity.getShareTenantName() + shareTenantName + ",");
				}
			}
			update(entity);
		} catch (Exception e) {
			transactionManager.rollback(status);
			throw e;
		}
		transactionManager.commit(status);
		return result;
	}
}
